package org.jeecg.modules.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class SMSDmeo {

    //@Value("${application.smshead}")
    private static String smsHead = "【江宁公共自行车公司】";

    public static void main(String[] args) {
        send("13770865289","23223");
        sendMessage("13770865289","测试");
    }
    /**
     * 短信发送接口
     *
     * @return
     */
    public static String send(String mobiles, String code) {

        //请求参数
        String url = "http://sms.ymeeting.cn:7862/sms?action=send";
        String action = "send";
        String account = "923223";
        String password = "znJzEe";
        String mobile = mobiles;
        String content = smsHead + "验证码" + code + "，用于注册/登录，10分钟内有效。验证码提供给他人可能导致账号被盗，请勿泄漏，谨防被骗。";
        String extno = "106903223";//接入码
        String rt = "json";
        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("action", action);
        requestMap.put("account", account);
        requestMap.put("password", md5Encrypt(password + extno + content + mobile)); //MD5加密
        requestMap.put("mobile", mobile);
        requestMap.put("content", content);
        requestMap.put("extno", extno);
        requestMap.put("rt", rt);
        //返回JSON字符串
        String requestResult = null;
        try {
            //发送请求
            requestResult = HttpclientUtil.post(url, requestMap, "UTF-8");
        } catch (NetServiceException e) {
            e.printStackTrace();

        }

        return requestResult;
    }

    /**
     * 短信发送接口
     *
     * @param mobiles 手机号  message 发送内容
     */
    public static String sendMessage(String mobiles, String message) {

        //请求参数
        String url = "http://sms.ymeeting.cn:7862/sms?action=send";
        String action = "send";
        String account = "923223";
        String password = "znJzEe";
        String mobile = mobiles;
        String content = smsHead + message;
        String extno = "106903223";//接入码
        String rt = "json";
        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("action", action);
        requestMap.put("account", account);
        requestMap.put("password", md5Encrypt(password + extno + content + mobile)); //MD5加密
        requestMap.put("mobile", mobile);
        requestMap.put("content", content);
        requestMap.put("extno", extno);
        requestMap.put("rt", rt);
        //返回JSON字符串
        String requestResult = null;
        try {
            //发送请求
            requestResult = HttpclientUtil.post(url, requestMap, "UTF-8");
        } catch (NetServiceException e) {
            e.printStackTrace();
        }

        return requestResult;
    }

    /**
     * 点对点发送接口
     *
     * @return
     */
    public String p2p() {
        //请求地址
        String url = "http://sms.ymeeting.cn:7862/sms?action=p2p";
        //请求参数
        String action = "p2p";
        String account = "account";
        String password = "password";
        String mobileContentList = "18013863361#测试\n19905123812#测试\n19905123821#测试";
        String mobileContentListEncrypt = "18013863361#测试19905123812#测试19905123821#测试";//去掉换行符用于加密
        String extno = "106903396";
        String rt = "json";
        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("action", action);
        requestMap.put("account", account);
        requestMap.put("password", md5Encrypt(password + extno + mobileContentListEncrypt)); //MD5加密
        requestMap.put("mobileContentList", mobileContentList);
        requestMap.put("extno", extno);
        requestMap.put("rt", rt);
        //返回JSON字符串
        String requestResult = null;
        try {
            //发送请求
            requestResult = HttpclientUtil.post(url, requestMap, "UTF-8");
        } catch (NetServiceException e) {
            e.printStackTrace();
        }

        return requestResult;
    }


    /**
     * 余额查询接口
     *
     * @return
     */
    public String overage() {
        //请求地址
        String url = "http://sms.ymeeting.cn:7862/sms?action=overage";
        //请求参数
        String action = "balance";
        String account = "account";
        String password = "password";
        String rt = "json";
        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("action", action);
        requestMap.put("account", account);
        requestMap.put("password", md5Encrypt(password)); //MD5加密
        requestMap.put("rt", rt);
        //返回JSON字符串
        String requestResult = null;
        try {
            //发送请求
            requestResult = HttpclientUtil.post(url, requestMap, "UTF-8");
        } catch (NetServiceException e) {
            e.printStackTrace();
        }

        return requestResult;
    }


    /**
     * 客户端主动获取状态报告接口
     *
     * @return
     */
    public String report() {
        //请求地址
        String url = "http://sms.ymeeting.cn:7862/sms?action=report";
        //请求参数
        String action = "report";
        String account = "account";
        String password = "password";
        String size = "1000";
        String rt = "json";
        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("action", action);
        requestMap.put("account", account);
        requestMap.put("password", md5Encrypt(password)); //MD5加密
        requestMap.put("size", size);
        requestMap.put("rt", rt);
        //返回JSON字符串
        String requestResult = null;
        try {
            //发送请求
            requestResult = HttpclientUtil.post(url, requestMap, "UTF-8");
        } catch (NetServiceException e) {
            e.printStackTrace();
        }

        return requestResult;
    }


    /**
     * 客户端主动获取手机上行接口
     *
     * @return
     */
    public String mo() {
        //请求地址
        String url = "http://sms.ymeeting.cn:7862/sms?action=mo";
        //请求参数
        String action = "mo";
        String account = "account";
        String password = "password";
        String size = "1000";
        String rt = "json";
        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("action", action);
        requestMap.put("account", account);
        requestMap.put("password", md5Encrypt(password)); //MD5加密
        requestMap.put("size", size);
        requestMap.put("rt", rt);
        //返回JSON字符串
        String requestResult = null;
        try {
            //发送请求
            requestResult = HttpclientUtil.post(url, requestMap, "UTF-8");
        } catch (NetServiceException e) {
            e.printStackTrace();
        }

        return requestResult;
    }

    /**
     * MD5加密
     *
     * @param signContent
     * @return
     */
    public static String md5Encrypt(String signContent) {
        String hashResult = "";

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(signContent.getBytes("UTF-8"));
            byte byteData[] = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            hashResult = sb.toString().toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("No Such Algorithm.");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unsupported encoding type.");
        }

        return hashResult;
    }


}
